package com.sp.api.springbootapi.rabbitmq.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by weidewang on 2018/7/11
 */
public class TopicSend {
    public static final String EXCHANGE_NAME = "test_exchange_topic";

    public static void main(String[] args) {

        Connection connection = null;
        final Channel channel;
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            factory.setUsername("guest");
            factory.setPassword("guest");
            factory.setPort(5672);
            connection = factory.newConnection();
            channel = connection.createChannel();

            channel.exchangeDeclare(EXCHANGE_NAME,"topic");

            String message = "商品add...";
            channel.basicPublish(EXCHANGE_NAME,"goods.add",null,message.getBytes());
//            channel.basicPublish(EXCHANGE_NAME,"goods.update",null,message.getBytes());
//            channel.basicPublish(EXCHANGE_NAME,"goods.add1",null,message.getBytes());

            System.out.println(" [x] Sent'"+message+"'" );
            channel.close();


        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } finally {
            if (connection!=null){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
